/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.wallet.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import com.pig4cloud.pigx.wallet.constant.WalletConstant;
import com.pig4cloud.pigx.wallet.entity.Account;
import com.pig4cloud.pigx.wallet.entity.Currency;
import com.pig4cloud.pigx.wallet.service.AccountService;
import com.pig4cloud.pigx.wallet.service.CurrencyService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 币种列表
 *
 * @author
 * @date 2022-08-16 15:25:53
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/currency" )
@Api(value = "currency", tags = "币种列表管理")
public class CurrencyController {

    private final  CurrencyService currencyService;

	private final AccountService accountService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param currency 币种列表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('wallet_currency_view')" )
    public R getCurrencyPage(Page page, Currency currency) {
        return R.ok(currencyService.page(page, Wrappers.query(currency)));
    }


    /**
     * 通过id查询币种列表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('wallet_currency_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(currencyService.getById(id));
    }

    /**
     * 新增币种列表
     * @param currency 币种列表
     * @return R
     */
    @ApiOperation(value = "新增币种列表", notes = "新增币种列表")
    @SysLog("新增币种列表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('wallet_currency_add')" )
    public R save(@RequestBody Currency currency) {
        return R.ok(currencyService.save(currency));
    }

    /**
     * 修改币种列表
     * @param currency 币种列表
     * @return R
     */
    @ApiOperation(value = "修改币种列表", notes = "修改币种列表")
    @SysLog("修改币种列表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('wallet_currency_edit')" )
    public R updateById(@RequestBody Currency currency) {
        return R.ok(currencyService.updateById(currency));
    }

    /**
     * 通过id删除币种列表
     * @param
     * @return R
     */
    @ApiOperation(value = "通过id删除币种列表", notes = "通过id删除币种列表")
    @SysLog("通过id删除币种列表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('wallet_currency_del')" )
    public R removeById(@RequestBody List<Long> idList) {
        return R.ok(currencyService.removeBatchByIds(idList));
    }


    /**
     * 导出excel 表格
     * @param currency 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('wallet_currency_export')" )
    public List<Currency> export(Currency currency) {
        return currencyService.list(Wrappers.query(currency));
    }

	/**
	 * 获取充值页面数据
	 */
//	@Inner(false)
	@ApiOperation(value = "获取充值页面数据", notes = "获取充值页面数据")
	@PostMapping("/rechargePage")
	public R rechargePage(@RequestBody Account account){

		Long userId = SecurityUtils.getUser().getId();

		Account accountServiceOne = accountService.getOne(new LambdaQueryWrapper<Account>()
				.eq(Account::getMainNetwork, account.getMainNetwork())
				.eq(Account::getUserId, userId)
		);

		Currency currency = currencyService.getOne(new LambdaQueryWrapper<Currency>()
				.eq(Currency::getPublicChain, account.getMainNetwork())
				.eq(Currency::getState, WalletConstant.TRUE)
				.eq(Currency::getName, WalletConstant.USDT)
		);
		currency.setAddress(accountServiceOne.getAddress());

		return R.ok(currency);
	}

	/**
	 * 获取提现页面数据
	 */
//	@Inner(false)
	@ApiOperation(value = "获取提现页面数据", notes = "获取提现页面数据")
	@PostMapping("/withdrawalPage")
	public R withdrawalPage(@RequestBody Account account){

		Currency currency = currencyService.getOne(new LambdaQueryWrapper<Currency>()
				.eq(Currency::getPublicChain, account.getMainNetwork())
				.eq(Currency::getState, WalletConstant.TRUE)
				.eq(Currency::getName, WalletConstant.USDT)
		);

		return R.ok(currency);
	}

	/**
	 * 获取所有 未禁用/未逻辑删除的  币种
	 * 字段需求： id，name
	 */
	@Inner
	@ApiOperation(value = "获取所有 未禁用/未逻辑删除的  币种", notes = "获取所有 未禁用/未逻辑删除的  币种")
	@GetMapping("/contract/list")
	public R contractList(){
		List<Currency> list = currencyService.list(new LambdaQueryWrapper<Currency>()
				.eq(Currency::getState, WalletConstant.TRUE)
		);
		return R.ok(list);
	}
}
